#include "view_others.h"
#include "display_ctrl.h"
#include "platform_assert.h"
#include "lfs_file_manager_auto.h"
#include "rtc.h"
#include "datetime.h"
#include "blecom.h"
#include "battery_ctrl.h"
#include "sport_his.h"
#include "display_interface_240x240.h"
#include "datetime.h"
#include "heart_rate_interface.h"
#include "font/num_font.h"
#include "view_others_device_info.h"
#include "view_common.h"
#include "view_others_stopwatch.h"
#include "Motor.h"
#include "view.h"
#include "times/view_times.h"
#include "sports_mode.h"
#include "app_trace.h"
#include "device_type.h"
#define LOG(fmt, ...) app_trace_log(fmt "\r\n", ##__VA_ARGS__)
void view_others_app_url_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.com_data.i_temp[0] = get_device_type();
        disp_ctrl.auto_refresh_time = 1000;
    }
    if (disp_ctrl.com_data.i_temp[0] == device_type_read)
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 55, 43, FILE_IMG_Read_DownloadUrl_55_43);
    }
    else
    {
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 55, 43, FILE_IMG_9409_hard);
    }
    disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_AppDownload, 50);
}
void view_others_back_display(void)
{
    int x = 67;
    int y = 40;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_other_back_67_40);
    disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_otherBack, 50);
}

void view_others_base_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        disp_ctrl.auto_refresh_time = 500;
    }
    int x = 76;
    int y = 42;
    img_gif_com_t const img_cfg = {
        .gif_count = 3,
        .img[0].x = x,
        .img[0].y = y,
        .img[0].sn = FILE_IMG_F501_other_icon_1_76_42,
        .img[1].x = x,
        .img[1].y = y,
        .img[1].sn = FILE_IMG_F502_other_icon_2_76_42,
        .img[2].x = x,
        .img[2].y = y,
        .img[2].sn = FILE_IMG_F503_other_icon_3_76_42,
    };
    _disp_show_flash_img_gif_all(&img_cfg, DISP_LFS_FILE_INDEX_0);
    // x = 94;
    // y = 156;
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_F505_other_text_94_156);
    disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_Other, 50);
}
void view_others_camera_display(void)
{
    int x = 68;
    int y = 55;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_F101_camera_icon_68_55);
    // x = 85;
    // y = 165;
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_F102_camera_text_85_165);
    disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_Camera, 50);
}
void view_others_camera_shake_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        bool connect = ble_if_connected();
        disp_ctrl.com_data.i_temp[1] = battery_ctrl_get_level(battery_ctrl_get_percent());
        disp_ctrl.com_data.i_temp[2] = battery_ctrl_if_charging();
        disp_ctrl.auto_refresh_time = 500;
        disp_ctrl.auto_close_delay = 1000;
        if (!connect && disp_ctrl.com_data.i_temp[0] != connect)
        {
            motor_start(100);
            disp_ctrl.auto_refresh_count = 0;
            disp_ctrl.auto_refresh_time = 0;
        }
        disp_ctrl.com_data.i_temp[0] = connect;
        if (connect)
        {
            display_set_update_area(70, 49, 100, 102);
        }
        else
        {
            disp_ctrl.area_update_flag = 0;
        }
    }
    if (!disp_ctrl.com_data.i_temp[0])
    {
        if (disp_ctrl.auto_refresh_count > 6)
        {
            display_show(view_others_camera, DISP_MODE_NORMAL);
            return;
        }
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 71, 43, FILE_IMG_T102_nocon_71_43);
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 52, 147, FILE_IMG_T103_nocon_52_147);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_AlertNotConnected, 50);
        return;
    }
    int x = 83;
    int y = 10;
    if (disp_ctrl.com_data.i_temp[0])
    {
        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_connect);
    }
    else
    {
        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_disconnect);
    }
    x = 123;
    y = 12;
    disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_battery_back);
    if (disp_ctrl.com_data.i_temp[2])
    {
        disp_draw_user_image_ignore_color(x + 9, y + 0, RGB565_COLOR_BLACK, bmp_battery_charging);
    }
    else
    {
        uint16_t color = 0x9cd3;
        if (disp_ctrl.com_data.i_temp[1] == 1)
        {
            color = 0x9020;
        }
        x += 2;
        y += 2;
        for (int i = 0; i < disp_ctrl.com_data.i_temp[1]; i++)
        {
            disp_draw_rect_color(x, y, x + 4 - 1, y + 8 - 1, color);
            x += 5;
        }
    }
    x = 70;
    y = 49;
    // 58 54
    const img_gif_com_t img_cfg = {
        .gif_count = 2,
        .img[0].x = x,
        .img[0].y = y,
        .img[0].sn = FILE_IMG_F103_camera_shake_1_70_49,
        .img[1].x = x,
        .img[1].y = y,
        .img[1].sn = FILE_IMG_F104_camera_shake_2_70_49,
    };
    _disp_show_flash_img_gif_all(&img_cfg, DISP_LFS_FILE_INDEX_0);
    if (disp_ctrl.auto_refresh_count > 4)
    {
        display_show(view_others_camera_static, DISP_MODE_NORMAL);
    }
    // x = 46;
    // y = 164;
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_F105_camera_shake_text_46_164);
    disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_Shake, 50);
}
void view_others_camera_static_display(void)
{
    if (disp_ctrl.loop_cnt == 0)
    {
        bool connect = ble_if_connected();
        disp_ctrl.com_data.i_temp[1] = battery_ctrl_get_level(battery_ctrl_get_percent());
        disp_ctrl.com_data.i_temp[2] = battery_ctrl_if_charging();
        disp_ctrl.auto_refresh_time = 1000;
        disp_ctrl.auto_close_delay = 2000;
        if (!connect && disp_ctrl.com_data.i_temp[0] != connect)
        {
            motor_start(100);
            disp_ctrl.auto_refresh_count = 0;
        }
        disp_ctrl.com_data.i_temp[0] = connect;
        if (disp_ctrl.auto_refresh_count > 5)
        {
            display_show(view_others_camera, DISP_MODE_NORMAL);
            return;
        }
    }
    if (!disp_ctrl.com_data.i_temp[0])
    {
        if (disp_ctrl.auto_refresh_count > 3)
        {
            display_show(view_others_camera, DISP_MODE_NORMAL);
            return;
        }
        _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, 71, 43, FILE_IMG_T102_nocon_71_43);
        // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, 52, 147, FILE_IMG_T103_nocon_52_147);
        disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_AlertNotConnected, 50);
        return;
    }
    int x = 83;
    int y = 10;
    if (disp_ctrl.com_data.i_temp[0])
    {
        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_connect);
    }
    else
    {
        disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_disconnect);
    }
    x = 123;
    y = 12;
    disp_draw_user_image_ignore_color(x, y, RGB565_COLOR_BLACK, bmp_time_0_battery_back);
    if (disp_ctrl.com_data.i_temp[2])
    {
        disp_draw_user_image_ignore_color(x + 9, y + 0, RGB565_COLOR_BLACK, bmp_battery_charging);
    }
    else
    {
        uint16_t color = 0x9cd3;
        if (disp_ctrl.com_data.i_temp[1] == 1)
        {
            color = 0x9020;
        }
        x += 2;
        y += 2;
        for (int i = 0; i < disp_ctrl.com_data.i_temp[1]; i++)
        {
            disp_draw_rect_color(x, y, x + 4 - 1, y + 8 - 1, color);
            x += 5;
        }
    }

    x = 70;
    y = 49;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_F106_camera_shake_3_70_49);
    // x = 46;
    // y = 164;
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_F105_camera_shake_text_46_164);
    disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_Shake, 50);
}

void view_others_power_off_display(void)
{
    int x = 75;
    int y = 48;
    _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_F401_poweroff_icon_75_48);
    // x = 75;
    // y = 175;
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, FILE_IMG_F402_poweroff_text_75_65);
    disp_show_localize(DISP_LFS_FILE_INDEX_1, localize_string_key_PowerOff, 50);
}
void view_others_power_off_confirm_display(void)
{
    // int x = 75;
    int y = 65;
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_0, x, y, FILE_IMG_F402_poweroff_text_75_65);
    disp_show_localize_with_x_y(DISP_LFS_FILE_INDEX_0, localize_string_key_PowerOff, 50, -1, y);
    localize_string_key_t first_index = (localize_string_key_t)0;
    localize_string_key_t second_index = (localize_string_key_t)0;
    // int first_x = 0;
    // int second_x = 0;
    if (disp_ctrl.com_data.i_temp[0] == 0)
    {
        first_index = localize_string_key_powerOffYes;
        // first_x = 103;
        second_index = localize_string_key_powerOffNo;
        // second_x = 107;
    }
    else
    {
        second_index = localize_string_key_powerOffYes;
        // second_x = 103;
        first_index = localize_string_key_powerOffNo;
        // first_x = 107;
    }
    // x = first_x;
    y = 106;
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_1, x, y, first_index);
    disp_show_localize_with_x_y(DISP_LFS_FILE_INDEX_1, first_index, 51, -1, y);
    // x = second_x;
    y = 155;
    // _disp_show_flash_img(DISP_LFS_FILE_INDEX_2, x, y, second_index);
    disp_show_localize_with_x_y(DISP_LFS_FILE_INDEX_2, second_index, 52, -1, y);
    // 49 190
    for (int i = 49; i <= 190; i++)
    {
        disp_draw_point(i, 96, 0x3186);
        disp_draw_point(i, 139, 0x3186);
    }
}
view_event_result_t view_others_camera_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        if (ble_if_connected())
        {
            display_show(view_prompt_camera_ready, DISP_MODE_NORMAL);
            BraceletSendToAPPCamera(CAMERA_CMD_SLAVE_INTOCAMERA);
        }
        else
        {
            display_show(view_prompt_not_connect, DISP_MODE_NORMAL);
        }
        return view_event_result_handled;
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
        display_show(view_others_back - VIEW_LEVEL_1_SUB_COUNT, DISP_MODE_UP_MOVE);
        return view_event_result_handled;
#endif
    }
    return view_event_result_not_handled;
}
view_event_result_t view_others_camera_static_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
        return view_event_result_handled;
#ifdef EVENT_BUTTON_FRONT
#ifdef NO_BACK_BUTTON
    case event_button_front_long_press:
        display_show(DISP_TYPE_CAMERA, DISP_MODE_NORMAL);
        return view_event_result_handled;
#endif
#endif

    case event_view_exit:
        if (param->next_view != view_others_camera_shake)
        {
            BraceletSendToAPPCamera(CAMERA_CMD_SLAVE_LEVECAMERA);
            sports_mode_state_t state = (sports_mode_state_t)sports_mode_get_state();
            switch (state)
            {
            // case sports_mode_state_pause:
            //     display_show(view_sports_paused, DISP_MODE_NORMAL);
            //     return view_event_result_handled;
            case sports_mode_state_normal:
                display_show(view_sports_state_0, DISP_MODE_NORMAL);
                return view_event_result_handled;
            }
        }
        return view_event_result_handled;
    case event_shake:
        if (ble_if_connected())
        {
            BraceletSendToAPPCamera(CAMERA_CMD_SLAVE_STARTPHOTO);
            motor_start_extend(MOTOR_VIBRATE_TYPE_SHARK);
            display_show(view_others_camera_shake, DISP_MODE_NORMAL);
        }
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_others_camera_shake_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
        return view_event_result_handled;
    case event_view_exit:
        if (param->next_view != view_others_camera_static)
        {
            BraceletSendToAPPCamera(CAMERA_CMD_SLAVE_LEVECAMERA);
            sports_mode_state_t state = (sports_mode_state_t)sports_mode_get_state();
            switch (state)
            {
            // case sports_mode_state_pause:
            //     display_show(view_sports_paused, DISP_MODE_NORMAL);
            //     return view_event_result_handled;
            case sports_mode_state_normal:
                display_show(view_sports_state_0, DISP_MODE_NORMAL);
                return view_event_result_handled;
            }
        }
        return view_event_result_handled;
    case event_shake:
        disp_ctrl.auto_refresh_count = 0;
        if (ble_if_connected())
        {
            BraceletSendToAPPCamera(CAMERA_CMD_SLAVE_STARTPHOTO);
            motor_start_extend(MOTOR_VIBRATE_TYPE_SHARK);
        }
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_others_device_info_detail_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
        return view_event_result_handled;
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef EVENT_LEVEL_UP
    case event_move_level_up:
#endif
        display_show(view_others_device_info, DISP_MODE_NORMAL);
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_others_app_url_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_others_power_off_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
        display_show(view_others_camera, DISP_MODE_DOWN_MOVE);
        return view_event_result_handled;
#endif
        // case event_move_level_down:
        //     motor_start(500);
        //     system_into_poweroff();
        //     return view_event_result_handled;
    }
    return view_event_result_not_handled;
}
view_event_result_t view_others_power_off_confirm_event(event_param_t const *param)
{
    switch (param->event)
    {
    case event_view_entry:
        disp_ctrl.com_data.i_temp[0] = 0;
        return view_event_result_handled;
#ifdef EVENT_PREVIOUS_VIEW
    case event_move_previous_view:
#endif
#ifdef EVENT_NEXT_VIEW
    case event_move_next_view:
#endif
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
#endif
        disp_ctrl.com_data.i_temp[0] = !disp_ctrl.com_data.i_temp[0];
        display_set_update_request(view_others_power_off_confirm, 0);
        return view_event_result_handled;
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_long_press:
#endif
#ifdef EVENT_LEVEL_DOWN
    case event_move_level_down:
#endif
        if (disp_ctrl.com_data.i_temp[0] == 0)
        {
            motor_start(500);
            system_into_poweroff();
        }
        else
        {
            display_show(view_others_power_off, DISP_MODE_NORMAL);
        }
        return view_event_result_handled;
    }
    return view_event_result_not_handled;
}

view_event_result_t view_others_back_event(event_param_t const *param)
{
    switch (param->event)
    {
#ifdef EVENT_BUTTON_FRONT
    case event_button_front_press:
        display_show(view_others_level_1_begin, DISP_MODE_DOWN_MOVE);
        return view_event_result_handled;
    case event_button_front_long_press:
        display_show(view_others, DISP_MODE_NORMAL);
        return view_event_result_handled;
#endif
    }
    return view_event_result_not_handled;
}

void view_others_display(int32_t view)
{
    Expects(view >= view_others);
    Expects(view < view_others + VIEW_LEVEL_0_SUB_COUNT);
    switch (view)
    {
    case view_others_base:
        view_others_base_display();
        break;
    case view_others_camera:
        view_others_camera_display();
        break;
#ifdef NO_STOPWATCH_UI
#else
    case view_others_stopwatch:
        view_others_stopwatch_display();
        break;
#endif
    case view_others_device_info:
        view_others_device_info_display();
        break;
    case view_others_power_off:
        view_others_power_off_display();
        break;
    case view_others_power_off_confirm:
        view_others_power_off_confirm_display();
        break;
    case view_others_camera_static:
        view_others_camera_static_display();
        break;
    case view_others_camera_shake:
        view_others_camera_shake_display();
        break;
    case view_others_device_info_detail:
        view_others_device_info_detail_display();
        break;
#ifdef NO_APP_URL_UI
#else
    case view_others_app_url:
        view_others_app_url_display();
        break;
#endif
    case view_others_back:
        view_others_back_display();
        break;
    }
}

view_event_result_t view_others_event(int32_t view, event_param_t const *param)
{
    Expects(view >= view_others);
    Expects(view < view_others + VIEW_LEVEL_0_SUB_COUNT);
    view_event_result_t result = view_event_result_not_handled;
    switch (view)
    {
#ifdef NO_STOPWATCH_UI
#else
    case view_others_stopwatch:
        result = view_others_stopwatch_event(param);
        break;
#endif
    case view_others_camera:
        result = view_others_camera_event(param);
        break;
    case view_others_camera_static:
        result = view_others_camera_static_event(param);
        break;
    case view_others_camera_shake:
        result = view_others_camera_shake_event(param);
        break;
    case view_others_device_info_detail:
        result = view_others_device_info_detail_event(param);
        break;
#ifdef NO_APP_URL_UI
#else
    case view_others_app_url:
        result = view_others_app_url_event(param);
        break;
#endif
    case view_others_power_off:
        result = view_others_power_off_event(param);
        break;
    case view_others_power_off_confirm:
        result = view_others_power_off_confirm_event(param);
        break;
    case view_others_back:
        result = view_others_back_event(param);
        break;
    }
    if (result == view_event_result_handled)
    {
        return result;
    }
    return view_event_result_not_handled;
}
